package midone.exchangeskill.utils;

import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Rx工具库
 * Created by 想法的猫 on 2016/7/20.
 */
public class RxUtils {

    static RxUtils rxUtils;

    private RxUtils() {
    }

    public static RxUtils newRx() {
        if (rxUtils == null) {
            rxUtils = new RxUtils();
        }
        return rxUtils;
    }

    /**
     * 文本倒计时
     *
     * @param textView
     * @return
     */
    public static Subscription countDown(final TextView textView) {
        final int[] count = {60};
        textView.setText(count[0] + "s重新发送");
        return Observable.timer(1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).repeat(count[0]).subscribe(new Subscriber<Long>() {

            @Override
            public void onStart() {
                super.onStart();
                textView.setEnabled(false);
            }

            @Override
            public void onCompleted() {
                textView.setText("获取校验码");
                textView.setEnabled(true);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Long aLong) {
                count[0]--;
                textView.setText(count[0] + "s重新发送");
            }
        });
    }

    /**
     * 延迟1s跳转
     *
     * @param context
     * @param toClass
     */
    public static void delayStartActivity(final Activity context, final Class toClass) {
        Observable.timer(1, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                context.startActivity(new Intent(context, toClass));
                context.finish();
            }
        });
    }

    public static void delayStartActivity(final Activity context, final Class toClass, int milliSeconds) {
        Observable.timer(milliSeconds, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                context.startActivity(new Intent(context, toClass));
                context.finish();
            }
        });
    }


    private Subscriber delaySubscriber;
    Subscription subscription;

    public interface DelayCall {
        void onCall();
    }

    public Subscription debouce(final DelayCall delayCall) {
        if (delaySubscriber == null || subscription.isUnsubscribed()) {
            subscription = Observable.create(new Observable.OnSubscribe<DelayCall>() {
                @Override
                public void call(Subscriber<? super DelayCall> subscriber) {
                    delaySubscriber = subscriber;
                }
            }).debounce(500, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<DelayCall>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onNext(DelayCall call) {
                    call.onCall();
                }
            });
        }
        if (delaySubscriber != null) {
            delaySubscriber.onNext(delayCall);
        }
        return subscription;
    }

}
